<template>
  <div class="com-container">
    <div class="com-chart" ref="rank"></div>
  </div>
</template>

<script>
export default {
  created() {
    // 在组件创建完成后，进行回调函数注册
    this.$socket.registerCallBack("rankData", this.getData);
  },
  mounted() {
    // 初始化Charts
    this.initChart();
    // 获取数据
    // this.getData();
    // 告诉服务器，我现在需要数据
    this.$socket.send({
      action: "getData",
      socketType: "rankData",
      chartName: "rank",
      value: "",
    });
    // 适配
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
    this.$socket.unRegisterCallBack("rankData");
  },
  data() {
    return {
      // 图表的实例对象
      chartInstance: null,
      // 从服务器中获取的所有数据
      allData: null,
      // 区域缩放的起点值
      startValue: 0,
      // 区域缩放的终点值
      endValue: 9,
      timeId: null,
    };
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank, "chalk");

      const initOption = {
        title: {
          text: "▎地区销售排行",
          left: 20,
          top: 20,
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        tooltip: {
          show: true,
          backgroundColor: "#222733",
          borderColor: "#666666",
          textStyle: {
            color: "#fff",
          },
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            type: "bar",
            itemStyle: {
              // 柱样式设置
              barBorderRadius: [33, 33, 0, 0], // 柱状圆角属性
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      // 鼠标移入后开启定时器滚动
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      //  鼠标移出后开启定时器滚动
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData(res) {
      // 获取地图的散点数据
      // const { data: res } = await this.$http.get("rank");
      this.allData = res;
      // 从大到小进行排序
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];
      // 所有省份的数据
      const provinceArr = this.allData.map((item) => {
        return item.name;
      });
      // 所有省份对应的销售金额
      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      let dataOption = {
        xAxis: {
          data: provinceArr,
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              // 柱样式设置
              // barBorderRadius: [33, 33, 0, 0], // 柱状圆角属性
              // 指明颜色渐变的方向
              // 指明不同百分比之下颜色的值
              color: (arg) => {
                let targeColor = null;
                if (arg.value > 300) {
                  targeColor = colorArr[0];
                } else if (arg.value > 200) {
                  targeColor = colorArr[1];
                } else {
                  targeColor = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  // 百分之0下的颜色值
                  {
                    offset: 0,
                    color: targeColor[0],
                  },
                  // 百分之100下的颜色值
                  {
                    offset: 1,
                    color: targeColor[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    // 浏览器大小发生变化会调用此方法
    screenAdapter() {
      let titleFontSize = (this.$refs.rank.offsetWidth / 100) * 3.6;

      let adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      this.timeId = setInterval(() => {
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.startValue++;
        this.endValue++;
        this.updateChart();
      }, 2000);
    },
  },
};
</script>

<style></style>
